/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.emp.solbrillante.sales.view;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import pe.emp.solbrillante.sale.Main;
import pe.emp.solbrillante.sales.bean.PaqueteCompra;
import pe.emp.solbrillante.sales.bean.Producto;
import pe.emp.solbrillante.sales.bean.Proveedor;

/**
 *
 * @author TacbaranNew
 */
public class JProveedor extends javax.swing.JDialog {

    /**
     * Creates new form JProveedor
     */
    public JProveedor(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
         //pintamos el fondo de la ventana
        ((JFondo)jventana).setImagen("fondo_subVentanal_II.jpg");

        ((JFondo)jtitulo).setImagen("BarraHorizontalTitulos.png");
        ((JFondo)jfiltro).setImagen("BarraHorizontalTitulosBlanco.png");
                
        
        //llenamos los arreglos
        ArrayList<Producto> productos = Main.myService.queryAllProductos();
        ArrayList<Proveedor> proveedores = Main.myService.queryAllProveedores();

          //recorremos desde 1 porque no queremos el nombre de la etiqueta
        for(int i=1; i<proveedores.size();i++ )
        {
            
//          this.jComboBox1.addItem(proveedores.get(i).getNombre());
//          combobox de objetos :  se sobreescribe el metodo toString() en proveedores
            this.jComboBox1.addItem(proveedores.get(i));
        
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jventana = new JFondo();
        btnModificar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableListaPaquetesProveedor = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jtitulo = new JFondo();
        jLabel3 = new javax.swing.JLabel();
        jfiltro = new JFondo();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Proveedor");
        setMaximumSize(new java.awt.Dimension(850, 550));
        setMinimumSize(new java.awt.Dimension(850, 550));
        setPreferredSize(new java.awt.Dimension(850, 550));

        jventana.setMaximumSize(new java.awt.Dimension(800, 500));
        jventana.setMinimumSize(new java.awt.Dimension(800, 500));
        jventana.setPreferredSize(new java.awt.Dimension(800, 500));

        btnModificar.setText("Actualizar");
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        jTableListaPaquetesProveedor.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTableListaPaquetesProveedor);

        jLabel2.setText("Lista de Paquetes por Proveedor");

        jtitulo.setMaximumSize(new java.awt.Dimension(267, 39));
        jtitulo.setMinimumSize(new java.awt.Dimension(267, 39));
        jtitulo.setPreferredSize(new java.awt.Dimension(267, 39));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setText("Proveedor");

        javax.swing.GroupLayout jtituloLayout = new javax.swing.GroupLayout(jtitulo);
        jtitulo.setLayout(jtituloLayout);
        jtituloLayout.setHorizontalGroup(
            jtituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jtituloLayout.createSequentialGroup()
                .addContainerGap(124, Short.MAX_VALUE)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(94, 94, 94))
        );
        jtituloLayout.setVerticalGroup(
            jtituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jtituloLayout.createSequentialGroup()
                .addContainerGap(13, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addContainerGap())
        );

        jfiltro.setMaximumSize(new java.awt.Dimension(267, 39));
        jfiltro.setMinimumSize(new java.awt.Dimension(267, 39));
        jfiltro.setPreferredSize(new java.awt.Dimension(267, 39));

        jComboBox1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Seleccione Provedor");

        javax.swing.GroupLayout jfiltroLayout = new javax.swing.GroupLayout(jfiltro);
        jfiltro.setLayout(jfiltroLayout);
        jfiltroLayout.setHorizontalGroup(
            jfiltroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jfiltroLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(8, Short.MAX_VALUE))
        );
        jfiltroLayout.setVerticalGroup(
            jfiltroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jfiltroLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jfiltroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jventanaLayout = new javax.swing.GroupLayout(jventana);
        jventana.setLayout(jventanaLayout);
        jventanaLayout.setHorizontalGroup(
            jventanaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jventanaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jventanaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jventanaLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
                    .addGroup(jventanaLayout.createSequentialGroup()
                        .addGroup(jventanaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jfiltro, javax.swing.GroupLayout.PREFERRED_SIZE, 323, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jventanaLayout.setVerticalGroup(
            jventanaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jventanaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jtitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addComponent(jfiltro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(43, 43, 43)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnModificar)
                .addGap(102, 102, 102))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jventana, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jventana, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:       
        Proveedor proveedor = (Proveedor)this.jComboBox1.getSelectedItem();
        MostarProductosDeProveedor(proveedor);        
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        // TODO add your handling code here:
        
//                    //int col = this.jTableListaPaquetesProveedor.getModel().getColumnCount();
//                    int fil = this.jTableListaPaquetesProveedor.getModel().getRowCount(); 
//                    System.out.println("nro rows : " + fil);
//                    
//                    for( int i=0; i<fil; i++)
//                    {
//                       int cod =   Integer.parseInt( jTableListaPaquetesProveedor.getModel().getValueAt(i, 0).toString() );
//                       int can =   Integer.parseInt( jTableListaPaquetesProveedor.getModel().getValueAt(i, 5).toString() );//getValueAt(i, 6) modificacion!!!!!
//                       
//                       System.out.print("codigo : "+cod+" cantidad : "+can);
//                       int res_insert = Main.myService.queryInsertaCompraPaquete(cod,can);
//                       System.out.println("insertada fila :" + i);                      
//                       
//                       System.out.print(res_insert);
//                    }

    }//GEN-LAST:event_btnModificarActionPerformed
    private void MostarProductosDeProveedor(Proveedor proveedorDeproducto) {
                       //Declaramos las clases necesarias para la conexion     
            
            DefaultTableModel modelo;              
            
            modelo = new DefaultTableModel(){
            boolean[] canEdit = new boolean [] { false, false,false, false, false, false, false , false};

            @Override
                         public boolean isCellEditable(int rowIndex, int columnIndex) {
                             return canEdit [columnIndex];
                         }

            };
            
            this.jTableListaPaquetesProveedor.setModel(modelo); 
            
            int idprov = proveedorDeproducto.getId();
            System.out.println(idprov);
            //ArrayList<Paquete> paquetesxproveedor = Main.myService.queryPaquetesxProveedor(proveedorDeproducto.getId());
            ArrayList<PaqueteCompra> paquetesxproveedor = Main.myService.queryPaquetesxProveedorS(proveedorDeproducto.getId());
                 
    
            modelo.addColumn("Cod. Prov");
            modelo.addColumn("Proveedor");
            modelo.addColumn("Producto");
            modelo.addColumn("Uni. Paquete");
            modelo.addColumn("Precio Paquete");
            modelo.addColumn("Stock Min.");
            modelo.addColumn("Stock Max.");
            modelo.addColumn("Stock Actual");
            
            
          
           System.out.println("size : " + paquetesxproveedor.size());
            for (int i = 0; i < paquetesxproveedor.size(); i++) {                           
                //Extraemos los datos del ResultSet nombre=nombre de la columna              
                Object[] fila = new Object[8];                
                
                //fila[0]=paquetesxproveedor.get(i).getCodigo();
                fila[0]=idprov;
                fila[1]=paquetesxproveedor.get(i).getNom_prov();
                fila[2]=paquetesxproveedor.get(i).getNom_prd();
                fila[3]=paquetesxproveedor.get(i).getUnixpaquete();
                fila[4]=paquetesxproveedor.get(i).getPrecioxpaquete();
                fila[5]=paquetesxproveedor.get(i).getStock_maximo();
                fila[6]=paquetesxproveedor.get(i).getStock_minimo();
                fila[7]=paquetesxproveedor.get(i).getCantidad();
                
                //System.out.println(paquetesxproveedor.get(i).getId()+"..."+paquetesxproveedor.get(i).getCantidad_actual());
                modelo.addRow(fila);     
              
            }   
 
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JProveedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JProveedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JProveedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JProveedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JProveedor dialog = new JProveedor(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnModificar;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTableListaPaquetesProveedor;
    private javax.swing.JPanel jfiltro;
    private javax.swing.JPanel jtitulo;
    private javax.swing.JPanel jventana;
    // End of variables declaration//GEN-END:variables


}
